<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHader @lists="lists" :list="list"></TodoHader>
    <TodoMain :list="showlist" @deltsk="del"></TodoMain>
    <TodoFoolert
      @clearDone="clearDoneFn"
      @changFn="changFn"
      :selected="sel"
      :list="showlist.length"
    ></TodoFoolert>
  </section>
</template>

<script>
// 1.0 样式引入
import './styles/base.css'
import './styles/index.css'

import TodoHader from './components/TodoHader'
import TodoMain from './components/TodoMain'
import TodoFoolert from './components/TodoFoolert'

export default {
  data() {
    return {
      list: JSON.parse(localStorage.getItem('listt')) || [],
      sel: 'All',
    }
  },
  components: {
    TodoHader,
    TodoMain,
    TodoFoolert,
  },
  methods: {
    // 添加列表，数据在谁哪里就在谁这操作
    lists(uname) {
      const id = this.list.length ? this.list.at(-1).id + 1 : 100
      this.list.push({
        id,
        name: uname,
        isDone: false,
      })
    },
    // 删除事件，使用ID，唯一标识
    del(id) {
      this.list = this.list.filter((item) => item.id !== id)
    },
    changFn(val) {
      this.sel = val
    },
    // 清除已完成
    clearDoneFn() {
      this.list = this.list.filter((item) => !item.isDone)
    },
  },
  computed: {
    // 完成任务，未完成任务
    showlist() {
      if (this.sel === 'All') {
        return this.list
      } else if (this.sel === 'yes') {
        return this.list.filter((item) => item.isDone)
      } else {
        return this.list.filter((item) => !item.isDone)
      }
    },
  },
  watch: {
    // 本地缓存
    list: {
      deep: true,
      handler() {
        localStorage.setItem('listt', JSON.stringify(this.list))
      },
    },
  },
}
</script>
